package com.wisdomparking.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

public class ToolValid {
	/**
	 * 匹配正则表达式
	 *
	 * @param regex
	 *            regex
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean match(String regex, String value) {
		Pattern pattern = Pattern.compile(regex);

		if (pattern.matcher(value).find()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 区分大小写
	 *
	 * @param regex
	 *            regex
	 * @param flags
	 *            flags
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean match(String regex, int flags, String value) {
		Pattern pattern = Pattern.compile(regex, flags);

		if (pattern.matcher(value).find()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 邮箱验证工具
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isEmail(String value) {
		String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 手机号码验证
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isMobile(String value) {
		String check = "^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 座机验证
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isTel(String value) {
		String check = "^\\d{3,4}-?\\d{7,9}$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 电话号码 包括移动电话和座机
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isPhone(String value) {
		String telcheck = "^\\d{3,4}-?\\d{7,9}$";
		String mobilecheck = "^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$";

		if (match(telcheck, Pattern.CASE_INSENSITIVE, value)
				|| match(mobilecheck, Pattern.CASE_INSENSITIVE, value)) {
			return true;
		} else
			return false;
	}

	/**
	 * @param value
	 *            输入内容限制为英文字母 、数字和下划线
	 * @return boolean
	 */
	public static boolean isGeneral(String value) {
		String check = "^\\w+$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	public static boolean isGeneral(String value, int min, int max) {
		String check = "^\\w{" + min + "," + max + "}$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 判断是否是生日
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isBirthDay(String value) {
		String check = "(\\d{4})(/|-|\\.)(\\d{1,2})(/|-|\\.)(\\d{1,2})$";

		if (match(check, Pattern.CASE_INSENSITIVE, value)) {
			int year = Integer.parseInt(value.substring(0, 4));
			int month = Integer.parseInt(value.substring(5, 7));
			int day = Integer.parseInt(value.substring(8, 10));

			if (month < 1 || month > 12)
				return false;

			if (day < 1 || day > 31)
				return false;

			if ((month == 4 || month == 6 || month == 9 || month == 11)
					&& day == 31)
				return false;

			if (month == 2) {
				boolean isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
				if (day > 29 || (day == 29 && !isleap))
					return false;
			}
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 身份证
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isIdentityCard(String value) {
		String check = "(^\\d{15}$)|(^\\d{17}([0-9]|X)$)";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 邮政编码
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isZipCode(String value) {
		String check = "^[0-9]{6}$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 大于0的数字
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isNumber(String value) {
		String check = "^(\\+|\\-)?\\d+$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	public static boolean isNumber(String value, int min, int max) {
		String check = "^(\\+|\\-)?\\d{" + min + "," + max + "}$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 正整数
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isPositiveNumber(String value) {
		String check = "^\\d+$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	public static boolean isPositiveNumber(String value, int min, int max) {
		String check = "^\\d{" + min + "," + max + "}$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 中文
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isChinese(String value) {
		String check = "^[\\u2E80-\\u9FFF]+$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	public static boolean isChinese(String value, int min, int max) {
		String check = "^[\\u2E80-\\u9FFF]{" + min + "," + max + "}$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	/**
	 * 中文字、英文字母、数字和下划线
	 *
	 * @param value
	 *            value
	 * @return boolean
	 */
	public static boolean isString(String value) {
		String check = "^[\\u0391-\\uFFE5\\w]+$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	public static boolean isString(String value, int min, int max) {
		String check = "^[\\u0391-\\uFFE5\\w]{" + min + "," + max + "}$";
		return match(check, Pattern.CASE_INSENSITIVE, value);
	}

	
	public static boolean isNull(Object value){
		return value == null;
	}
	
	public static boolean isNull(Object ... value){
		if(isEmpty(value)){
			return true;
		}
		for (Object o : value) {
			if(o != null){
				return false;
			}
		}
		return true;
	}
	
	public static boolean isNullOr(Object ... value){
		if(isEmpty(value)){
			return true;
		}
		for (Object o : value) {
			if(o == null){
				return true;
			}
		}
		return false;
	}
	
	public static boolean isNotNull(Object value){
		return !isNull(value);
	}
	
	public static boolean isNotNull(Object ... value){
		return !isNullOr(value);
	}
	
	public static boolean isNotNullOr(Object ... value){
		return !isNull(value);
	}
	
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }
    
    public static boolean isBlank(String ... value) {
        if(isEmpty(value)){
            return true;
        }
        for (String str : value) {
            if(isNotBlank(str)) {
                return false;
            }
        }
        return true;
    }
    
    public static boolean isBlankOr(String ... value) {
        if(isEmpty(value)){
            return true;
        }
        for (String str : value) {
            if(isBlank(str)) {
                return true;
            }
        }
        return false;
    }
    
    public static boolean isNotBlank(String... value) {
        return !isBlankOr(value);
    }
    
    public static boolean isNotBlankOr(String ... value) {
        return !isBlank(value);
     }
    
    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }
	
	public static boolean isEmpty(Object value) {
		if(value instanceof Map<?,?>){
			if(value == null || ((Map<?,?>)value).isEmpty()){
				return true;
			}
		}
		if (value instanceof Collection) {
			if (value == null || ((Collection<?>) value).isEmpty()) {
				return true;
			}
		} else if (value instanceof String) {
			if (value == null || "".equals(value)) {
				return true;
			}
		} else if (value instanceof Object[]){
			if(value == null || ((Object[])value).length <= 0){
				return true;
			}
		} else {
			if (value == null)
				return true;
		}
		return false;
	}
	
	public static boolean isNotEmpty(Object value){
		return !isEmpty(value);
	}

	public static boolean isLength(String value, int min, int max) {
		int length = isEmpty(value) ? 0 : value.length();
		if (length >= min && length <= max)
			return true;
		else
			return false;
	}

	public static boolean compareDate(String date1, String date2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d1 = sdf.parse(date1);
			Date d2 = sdf.parse(date2);
			return d1.compareTo(d2) > 0;
		} catch (ParseException e) {
			return false;
		}
	}
	
	/**
     * 判断是否是车牌号
     */
	public static boolean isCarNo(String CarNum) {
		// 匹配第一位汉字
		String str = "京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼甲乙丙己庚辛壬寅辰戍午未申";
		if (!(CarNum == null || CarNum.equals(""))) {
			String s1 = CarNum.substring(0, 1);// 获取字符串的第一个字符
			if (str.contains(s1)) {
				String s2 = CarNum.substring(1, CarNum.length());
				// 不包含I O i o的判断
				if (s2.contains("I") || s2.contains("i") || s2.contains("O") || s2.contains("o")) {
					return false;
				} else {
					if (!CarNum.matches("^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$")) {
						return true;
					}
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
		return false;
	}

}
